package com.wxxymaker.edu.service;

import com.blade.ioc.annotation.Bean;
import com.blade.ioc.annotation.Inject;
import com.blade.kit.CollectionKit;
import com.wxxymaker.edu.model.entity.CountInfo;
import com.wxxymaker.edu.model.entity.User;
import io.github.biezhi.anima.Anima;
import io.github.biezhi.anima.page.Page;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * @AUTHOR soft
 * @DATE 2019/3/20 11:11
 * @DESCRIBE
 */
@Slf4j
@Bean
public class SysCountService {
    @Inject
    private UserService userService;
    @Inject
    private CourseService courseService;
    @Inject
    private PracticeService practiceService;
    @Inject
    private TestPaperService paperService;
    @Inject
    private TaskService taskService;
    @Inject
    private MaterialService materialService;
    @Inject
    private AskResultService resultService;
    @Inject
    private LearnCourseService learnCourseService;
    @Inject
    private LearnTaskService learnTaskService;
    @Inject
    private JoinTestPaperService joinTestPaperService;
    @Inject
    private NoteService noteService;
    @Inject
    private AskService askService;
    @Inject
    private CommentService commentService;
    @Inject
    private PracticeResponseService practiceResponseService;
    @Inject
    private AnnouncementService announcementService;

    /**
     * 分页查找用户统计信息
     * @param page 页码
     * @param limit 尺寸
     * @param user 条件 角色 姓名 性别
     */
    public Page<CountInfo> page(int page, int limit, User user) {
        Page<CountInfo> infoPage = null;
        if (user.getRole() != null && user.getRole() > 2) user.setRole(2);
        user.setAudit(1);
        Page<User> users = userService.page(page, limit, user);
        if (CollectionKit.isNotEmpty(users.getRows())) {
            infoPage = new Page<>(users.getTotalRows(), page, limit);
            List<CountInfo> infos = new ArrayList<>();
            for (User u : users.getRows()) {
                Integer userId = u.getId();
                CountInfo countInfo = new CountInfo();
                countInfo.setCourses(courseService.count(userId))
                        .setTasks(taskService.count(userId, null))
                        .setPractice(practiceService.countByUid(userId))
                        .setPapers(paperService.countByUid(userId))
                        .setMaterials(materialService.countByuId(userId))
                        .setAskResults(resultService.countByUid(userId))
                        .setPaperView(joinTestPaperService.countByUid(userId, true))
                        .setLearns(learnCourseService.countByUid(userId))
                        .setLooks(learnTaskService.countByUid(userId))
                        .setDoResponse(practiceResponseService.countByUid(userId))
                        .setExams(joinTestPaperService.countByUid(userId, false))
                        .setNotes(noteService.countByuId(userId))
                        .setAsks(askService.countByUid(userId))
                        .setComments(commentService.countByUid(userId))
                        .setUser(u);
                infos.add(countInfo);
            }

            infoPage.setRows(infos);
        }
        return infoPage;
    }

    /**
     * 清理课程相关信息
     * @param kid 课程id
     */
    public boolean clearCourse(Integer kid) {
        return Anima.atomic(()->{
            announcementService.deleteByKid(kid);
            taskService.deleteByKid(kid);
            learnTaskService.deleteByKid(kid);
            practiceService.deleteByKid(kid);
            paperService.deleteByKid(kid);
            joinTestPaperService.deleteByKid(kid);
            materialService.deleteByKid(kid);
            commentService.deleteByKid(kid);
            askService.deleteByKid(kid);
            noteService.deleteByKid(kid);
        }).catchException(e -> {
            log.error("清理课程数据失败,稍后清理... {}", e.getMessage());
        }).isRollback();
    }
}
